import Image
import os

##########################################################################################
#		TO DO
##########################################################################################

#	HANDLE OUTPUT PAPER SIZES OTHER THAN A4 AND A3
#	CREATE OUTPUT WITHOUT RELYING ON LATEX

#########################################################################################

#########################################################################################
#	COMPLETED
#########################################################################################

#	HANDLE LANDSCAPE IMAGES - ROTATE IMPLEMENTED INTERNALLY
#	HANDLE TILES OTHER THAN 2x2

#########################################################################################


##########################################################################################
#		USER INPUT
##########################################################################################

#WHAT IS THE PDFLATEX COMMAND?
cmd = 'pdflatex'

#WHAT IMAGE?
filename = "familylines.png"

#HOW MANY SHEETS DO YOU WANT TO SPLIT TO?
tiles_wide = 2
tiles_high = 2

#WHAT SIZE PAPER, EITHER "a4paper" or "a3paper"
paper = 'a3paper'

##########################################################################################

#TOTAL NUMBER OF TILES
tiles = tiles_wide * tiles_high

#SET GLOBAL TO STORE IF IMAGE IS PORTRAIT OR LANDSCAPE
portrait = -1

#OPEN FILE
im = Image.open(filename)
type = im.format.lower()
#SPLIT THE FILE EXTENSION OFF THE FILENAME
print "Filename: ", filename
im_name = filename.split('.')[0]

#FIND THE SIZE
size = im.size
width = size[0]
height = size[1]
print "Width: ",width, " Height: ", height

#WORK OUT IF PORTRAIT OR LANDSCAPE
if height > width :
	portrait = 1
	print "Image is: Portrait"
else:
	portrait = 0
	print "Image is: Landscape"
	print "Rotating..."
	# IF LANDSCAPE ROTATE THE IMAGE AND THEN
	# SET THE DIMENSIONS AGAIN
	im = im.rotate(90)
	width = im.size[0]
	height = im.size[1]

#WORK OUT THE SIZE OF THE IMAGES AND SPLIT
files=[]
c=0

for a in range(1,tiles_high+1):
	for b in range (1,tiles_wide+1):
		x0 = (b-1)*width/tiles_wide
		x1 = b*width/tiles_wide
		y0 = (a-1)*height/tiles_high
		y1 = a*height/tiles_high
		newname = im_name+"_" + str(c+1)+"."+type
		im.crop([x0,y0,x1,y1]).save(newname,type)
		files.append(newname);
		c=c+1

#SET VARIABLES TO SET IMAGE SIZE FOR PAPER SIZE
if paper == 'a4paper':
	height = 297 - 12
	width = 210 - 12
else:
	height = 420 - 12
	width = 297 - 12

#OPEN FILE FOR LATEX COMMANDS
latexfile = im_name+"_"+str(paper)+"_"+str(tiles)+".tex"
f = file(latexfile,'w')        # open for 'w'riting

#WRITE THE HEADER
preamble = "\documentclass{article}\r\n"
f.write(preamble)                   # write text to file
preamble = "\\usepackage{graphicx}\r\n\\usepackage[margin=0.3cm,paper="+str(paper)+"]{geometry}\r\n\\begin{document}\r\n"
f.write(preamble)

for a in range(0,tiles):
	contents = "\\includegraphics[width="+str(width)+"mm,height="+str(height)+"mm]{"+files[a]+"}\r\n\\clearpage\r\n"
	f.write(contents)

end = "\\end{document}"
f.write(end)
f.close()

#ADD THE LATEX FILENAME TO THE COMMAND
cmd = cmd + " " + latexfile
os.system(cmd)

#DELETE THE LATEX FILE
cmd = "rm "+latexfile
os.system(cmd)

#DELETE THE AUX FILE
cmd = "rm "+im_name+"_"+str(paper)+"_"+str(tiles)+".aux"
os.system(cmd)

#DELETE THE LOG FILE
cmd = "rm "+im_name+"_"+str(paper)+"_"+str(tiles)+".log"
os.system(cmd)
